#include "stdafx.h"
#include "ffmpeg_common_utils.h"

void make_ffmpeg_codec_audio_info_to_WAVEFORMATEX(AVCodecContext* codec,WAVEFORMATEX* pwfx)
{
	pwfx->wFormatTag = WAVE_FORMAT_UNKNOWN;
	pwfx->nChannels = codec->channels;
	pwfx->nSamplesPerSec = codec->sample_rate;
	pwfx->wBitsPerSample = av_get_bytes_per_sample(codec->sample_fmt) << 3;
	pwfx->nAvgBytesPerSec = (pwfx->nChannels * (pwfx->wBitsPerSample / 8)) * pwfx->nSamplesPerSec;
	pwfx->nBlockAlign = pwfx->nAvgBytesPerSec / pwfx->nSamplesPerSec;
	pwfx->cbSize = 0;
}

void make_ffmpeg_codec_audio_16bPCM_info_to_WAVEFORMATEX(AVCodecContext* codec,WAVEFORMATEX* pwfx)
{
	pwfx->wFormatTag = WAVE_FORMAT_PCM;
	pwfx->nChannels = 2;
	pwfx->nSamplesPerSec = codec->sample_rate;
	pwfx->wBitsPerSample = 16;
	pwfx->nAvgBytesPerSec = (pwfx->nChannels * (pwfx->wBitsPerSample / 8)) * pwfx->nSamplesPerSec;
	pwfx->nBlockAlign = pwfx->nAvgBytesPerSec / pwfx->nSamplesPerSec;
	pwfx->cbSize = 0;
}

bool query_ffmpeg_metadata_desc_string(AVDictionary* metadata,char* name,char* pstr,int cch)
{
	if (metadata == NULL || pstr == NULL)
		return false;
	memset(pstr,0,cch);
	AVDictionaryEntry* entry = av_dict_get(metadata,name,NULL,0);
	if (!entry)
		return false;
	if (!entry->value)
		return false;
	if (cch < strlen(entry->value))
		return true;
	strcpy(pstr,entry->value);
	return true;
}